//
//  SingleTablePopViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/4.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "SingleTablePopViewController.h"

#import "ACMacros.h"
#import "User.h"

@interface SingleTablePopViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation SingleTablePopViewController {
    __weak IBOutlet UIView *line;
    __weak IBOutlet UITableView *mainTableView;
    
    NSMutableArray *dataArray;
    NSInteger selectedRow;
    
    NSArray *industry;
    NSArray *zodiac;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self insertBackground];
    if (self.sourceArray || self.sourceArray.count == 0) {
        dataArray = [[NSMutableArray alloc]init];
    } else {
        dataArray = [[NSMutableArray alloc]initWithArray:self.sourceArray];
    }
    industry = @[@"互联网", @"电商", @"高新技术", @"消费品", @"房地产/建筑/装潢", @"医疗/保健", @"运输物流", @"政府及事业单位", @"教育/科研", @"服务业", @"人事招聘", @"广告公关", @"企业服务", @"媒体", @"文化艺术", @"法律", @"金融财务", @"餐饮/旅游/体育", @"制造加工页", @"其他"];
    zodiac = @[@"白羊座", @"金牛座", @"双子座", @"巨蟹座", @"狮子座", @"处女座", @"天秤座", @"天蝎座", @"射手座", @"摩羯座", @"水瓶座", @"双鱼座"];
    for (int i = 0; i < industry.count; i++) {
        if ([industry[i] isEqual:self.selectedItem]) {
            selectedRow = i;
        }
    }
    for (int i = 0; i < zodiac.count; i++) {
        if ([zodiac[i] isEqual:self.selectedItem]) {
            selectedRow = i;
        }
    }
    
    if ([self.popType isEqual:@"行业"]) {
        [dataArray removeAllObjects];
        [dataArray addObjectsFromArray:industry];
    } else if ([self.popType isEqual:@"星座"]) {
        [dataArray removeAllObjects];
        [dataArray addObjectsFromArray:zodiac];
    }
    [self.titleLabel setText:self.popType];
    [self configureTableView];

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)insertBackground {
    UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [backgroundImageView setImage:[UIImage imageNamed:@"screen_bg"]];
    [self.view insertSubview:backgroundImageView atIndex:0];
}

- (void)configureTableView {
    mainTableView.delegate = self;
    mainTableView.dataSource = self;
    [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setTintColor:[UIColor groupTableViewBackgroundColor]];
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:15]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell.textLabel setText:[dataArray objectAtIndex:[indexPath row]]];
    if (selectedRow == indexPath.row) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [cell.textLabel setTextColor:RGBCOLOR(255, 118, 108)];
    }
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [[tableView cellForRowAtIndexPath:indexPath].textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
    
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryNone];
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]].textLabel setTextColor:[UIColor whiteColor]];
    selectedRow = indexPath.row;
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryCheckmark];
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]].textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
    
}

- (IBAction)clickToCancelPop:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
    }];
}

- (IBAction)clickToDone:(UIButton *)sender {
    User *user = [User sharedUser];
    if ([self.popType isEqual:@"行业"]) {
        user.industry = industry[selectedRow];
    } else if ([self.popType isEqual:@"星座"]) {
        user.constellation = zodiac[selectedRow];
    }
    [self dismissViewControllerAnimated:true completion:^{
    }];

}

@end
